package com.hwk.dynamic.array;


import com.hwk.dynamic.List;

/**
 * 动态数组
 */
public class ArrayList<E> implements List<E> {
    /**
     * 真实个数
     */
    public int size;

    /**
     * 数组存储
     */
    private E[] elements;

    /**
     * 默认的容量
     */
    private static final int DEFAULT_CAPACITY = 10;

    /**
     * 找不到的返回下标
     */
    private static final int ELEMENT_NOT_FOUND = -1;

    /**
     * 此处构建
     */
    public ArrayList(int capacity){
        if(capacity < DEFAULT_CAPACITY){
            capacity = DEFAULT_CAPACITY;
        }
        elements = (E[]) new Object[capacity];
    }

    public ArrayList(){
        this(DEFAULT_CAPACITY);
    }

    @Override
    public int size() {
        return this.size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean contains(E element) {
        return indexOf(element)==ELEMENT_NOT_FOUND;
    }

    @Override
    public void add(E element) {
        this.add(size, element);
    }

    @Override
    public E get(int index) {
        this.rangeCheck(index);
        return elements[index];
    }

    @Override
    public E set(int index, E element) {
        this.rangeCheck(index);
        E old = elements[index];
        elements[index] = element;
        return old;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);

        ensureCapacity(size+1);

        for (int i = size-1; i >= index; i--) {
            elements[i+1] = elements[i];
        }
        elements[index] = element;
        size++;
    }

    @Override
    public E remove(int index) {
        this.rangeCheck(index);
        E old = elements[index];
        for (int i = index; i < size-1; i++) {
            elements[i] = elements[i+1];
        }
        elements[--size] = null;
        return old;
    }

    @Override
    public int indexOf(E element) {
        int index = ELEMENT_NOT_FOUND;
        for (int i = 0; i < elements.length; i++) {
            if(elements[i].equals(element)){
                index = i;
                break;
            }
        }
        return index;
    }

    @Override
    public void clear() {
        for (int i = 0; i < size; i++) {
            elements[i] = null;
        }
        size= 0;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        String rt = "";
        for (int i = 0; i < size; i++) {
            sb.append(elements[i].toString().concat(","));
        }
        rt = sb.toString().substring(0, sb.lastIndexOf(","));
        return "ArrayList{" +
                "size=" + size +
                ", elements=" + rt +
                '}';
    }

    /**
     * 保证要有capacity的容量
     * @param capacity
     */
    private void ensureCapacity(int capacity) {
        int oldCapacity = elements.length;
        if (oldCapacity >= capacity) return;

        // 新容量为旧容量的1.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        E[] newElements = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[i];
        }
        elements = newElements;

        System.out.println(oldCapacity + "扩容为" + newCapacity);
    }

    private void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            outOfBounds(index);
        }
    }

    private void rangeCheckForAdd(int index) {
        if (index < 0 || index > size) {
            outOfBounds(index);
        }
    }

    private void outOfBounds(int index) {
        throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size);
    }
}
